package cn.edu.dhu.acm.oj.client.panel;

import cn.edu.dhu.acm.oj.common.config.*;
import cn.edu.dhu.acm.oj.client.Control;

public class EnvironmentPanel extends javax.swing.JPanel {

    /** Creates new form EnvironmentPanel */
    public EnvironmentPanel(javax.swing.JDialog d) {
        eb = Control.getEnvBean();
        dialog = d;
        initComponents();
        try {
            OSname = System.getProperty("os.name");
            JTF_Cpp.setText(eb.getPath("Cpp"));
            JTF_C.setText(eb.getPath("C"));
            JTF_Java.setText(eb.getPath("Java"));
            JTF_Pascal.setText(eb.getPath("Pascal"));
            JTF_Work.setText(Control.getWorkpath());
            JTF_Tmp.setText(Control.getTmppath());
            target = new String[numLan];
            flag = new boolean[numLan];
            for (int i = 0; i < numLan; i++) {
                target[i] = eb.getCmd(LanguageList[i]) + ".exe";
                flag[i] = false;
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        JL_Message = new javax.swing.JLabel();
        JTF_Cpp = new javax.swing.JTextField();
        JTF_C = new javax.swing.JTextField();
        JTF_Java = new javax.swing.JTextField();
        JTF_Pascal = new javax.swing.JTextField();
        JTF_Work = new javax.swing.JTextField();
        JTF_Tmp = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        JB_OK = new javax.swing.JButton();
        JB_CppFind = new javax.swing.JButton();
        JB_CFind = new javax.swing.JButton();
        JB_JavaFind = new javax.swing.JButton();
        JB_PascalFind = new javax.swing.JButton();
        JB_Work = new javax.swing.JButton();
        JB_Tmp = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridLayout(7, 0));
        jPanel1.add(jLabel1);

        jLabel2.setText("Cpp-g++");
        jPanel1.add(jLabel2);

        jLabel3.setText("C-gcc");
        jPanel1.add(jLabel3);

        jLabel4.setText("Java-javac");
        jPanel1.add(jLabel4);

        jLabel5.setText("Pascal-fpc");
        jPanel1.add(jLabel5);

        jLabel6.setText("Work");
        jPanel1.add(jLabel6);

        jLabel7.setText("Tmp");
        jPanel1.add(jLabel7);

        add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setLayout(new java.awt.GridLayout(7, 0));

        JL_Message.setText("Click find to set your path ,like:   c:\\mingw\\bin\\   endwith\"\\\"");
        jPanel2.add(JL_Message);

        JTF_Cpp.setColumns(20);
        JTF_Cpp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JTF_CppActionPerformed(evt);
            }
        });
        jPanel2.add(JTF_Cpp);

        JTF_C.setColumns(20);
        jPanel2.add(JTF_C);

        JTF_Java.setColumns(20);
        jPanel2.add(JTF_Java);

        JTF_Pascal.setColumns(20);
        jPanel2.add(JTF_Pascal);
        jPanel2.add(JTF_Work);
        jPanel2.add(JTF_Tmp);

        add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.GridLayout(7, 0));

        JB_OK.setText("OK");
        JB_OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_OKActionPerformed(evt);
            }
        });
        jPanel3.add(JB_OK);

        JB_CppFind.setText("find");
        JB_CppFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_CppFindActionPerformed(evt);
            }
        });
        jPanel3.add(JB_CppFind);

        JB_CFind.setText("find");
        JB_CFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_CFindActionPerformed(evt);
            }
        });
        jPanel3.add(JB_CFind);

        JB_JavaFind.setText("find");
        JB_JavaFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_JavaFindActionPerformed(evt);
            }
        });
        jPanel3.add(JB_JavaFind);

        JB_PascalFind.setText("find");
        JB_PascalFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_PascalFindActionPerformed(evt);
            }
        });
        jPanel3.add(JB_PascalFind);

        JB_Work.setText("find");
        JB_Work.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_WorkActionPerformed(evt);
            }
        });
        jPanel3.add(JB_Work);

        JB_Tmp.setText("find");
        JB_Tmp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_TmpActionPerformed(evt);
            }
        });
        jPanel3.add(JB_Tmp);

        add(jPanel3, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents

    private void JTF_CppActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTF_CppActionPerformed
        
}//GEN-LAST:event_JTF_CppActionPerformed

    private void JB_JavaFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_JavaFindActionPerformed
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().toString();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setPath("Java", path);
            JTF_Java.setText(path);
        }
}//GEN-LAST:event_JB_JavaFindActionPerformed

    private void JB_OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_OKActionPerformed
        try {
            String path;
            path = JTF_Cpp.getText();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setPath("Cpp", path);

            path = JTF_C.getText();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setPath("C", path);

            path = JTF_Java.getText();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setPath("Java", path);

            path = JTF_Pascal.getText();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setPath("Pascal", path);

            path = JTF_Work.getText();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            Control.setWorkpath(path);

            path = JTF_Tmp.getText();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            Control.setTmppath(path);
            Control.CheckTmppath();
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            dialog.dispose();
        }
    }//GEN-LAST:event_JB_OKActionPerformed

    private void JB_CppFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_CppFindActionPerformed
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().toString();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setPath("Cpp", path);
            JTF_Cpp.setText(path);
        }
    }//GEN-LAST:event_JB_CppFindActionPerformed

    private void JB_CFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_CFindActionPerformed
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().toString();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setPath("C", path);
            JTF_C.setText(path);
        }
    }//GEN-LAST:event_JB_CFindActionPerformed

    private void JB_PascalFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_PascalFindActionPerformed
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().toString();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setPath("Pascal", path);
            JTF_Pascal.setText(path);
        }
    }//GEN-LAST:event_JB_PascalFindActionPerformed

    private void JB_WorkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_WorkActionPerformed
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().toString();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setSourceCodeTemp(path);
            JTF_Work.setText(path);
        }
    }//GEN-LAST:event_JB_WorkActionPerformed

    private void JB_TmpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_TmpActionPerformed
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().toString();
            if (path.charAt(path.length() - 1) != separator) {
                path += separator;
            }
            eb.setSourceTarget(path, path);
            JTF_Tmp.setText(path);
        }
    }//GEN-LAST:event_JB_TmpActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_CFind;
    private javax.swing.JButton JB_CppFind;
    private javax.swing.JButton JB_JavaFind;
    private javax.swing.JButton JB_OK;
    private javax.swing.JButton JB_PascalFind;
    private javax.swing.JButton JB_Tmp;
    private javax.swing.JButton JB_Work;
    private javax.swing.JLabel JL_Message;
    private javax.swing.JTextField JTF_C;
    private javax.swing.JTextField JTF_Cpp;
    private javax.swing.JTextField JTF_Java;
    private javax.swing.JTextField JTF_Pascal;
    private javax.swing.JTextField JTF_Tmp;
    private javax.swing.JTextField JTF_Work;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables
    private String OSname = "Unknow";
    private EnvironmentBean eb;
    private String target[];
    private String LanguageList[] = {"Cpp", "C", "Java", "Pascal"};
    private int numLan = LanguageList.length;
    private boolean flag[];
    private char separator = System.getProperty("file.separator").charAt(0);
    private javax.swing.JDialog dialog;
}
